package com.wa.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.wa.annotation.AllchipsLogProcessServiceType;
import com.wa.page.BaseLogEntity;
import com.wa.bean.BasicAllchipsLog;
import com.wa.dto.OperationLogInfoDto;
import com.wa.dto.OperatorInfoDto;
import com.wa.pojo.BasicAllchipsLogPojo;
import com.wa.service.AbstractAllchipsLogProcessService;
import com.wa.util.AllchipsLogUtils;
import com.wa.util.CommUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Map;

/**
 * 日志处理 -- 操作日志
 *
 * @Author Dawn
 * @Date 2021/10/27 16:13
 */
@Slf4j
@AllchipsLogProcessServiceType(logCategory = 20)
@Service
public class OperationAllchipsLogProcessServiceImpl extends AbstractAllchipsLogProcessService {

    /**
     * 日志处理
     *
     * @Author Dawn
     * @Date 2021/10/26 14:36
     */
    @Override
    public void processLog0(Object dataObject, BasicAllchipsLogPojo basicAllchipsLogPojo, OperatorInfoDto operatorInfoDto) {
        BaseLogEntity baseLogEntity = (BaseLogEntity) dataObject;
        operatorInfoDto = OperatorInfoDto.process(operatorInfoDto, baseLogEntity.getOperatorInfoDto());

        basicAllchipsLogPojo.setBasicAllchipsLogs(new ArrayList<>());
        if (CollUtil.isNotEmpty(baseLogEntity.getOperationLogInfoList())) {
            for (OperationLogInfoDto operationLogInfoDto : baseLogEntity.getOperationLogInfoList()) {
                BasicAllchipsLog basicAllchipsLog = this.buildLog(operationLogInfoDto, operatorInfoDto);
                if (basicAllchipsLog != null) {
                    basicAllchipsLogPojo.getBasicAllchipsLogs().add(basicAllchipsLog);
                }
            }
        }
        if (CommUtils.isNotNull(baseLogEntity.getOperationLogInfo())) {
            BasicAllchipsLog basicAllchipsLog = this.buildLog(baseLogEntity.getOperationLogInfo(), operatorInfoDto);
            if (basicAllchipsLog != null) {
                basicAllchipsLogPojo.getBasicAllchipsLogs().add(basicAllchipsLog);
            }
        }
    }

    /**
     * 构建日志对象
     *
     * @Author Dawn
     * @Date 2021/10/28 11:25
     */
    public BasicAllchipsLog buildLog(OperationLogInfoDto operationLogInfoDto, OperatorInfoDto operatorInfoDto) {
        if (operationLogInfoDto == null) {
            return null;
        }
        BasicAllchipsLog basicAllchipsLog = new BasicAllchipsLog(operatorInfoDto);
        basicAllchipsLog.setEntityId(operationLogInfoDto.getEntityId());
        basicAllchipsLog.setEntityName(operationLogInfoDto.getEntityName());
        basicAllchipsLog.setDesc(this.getDesc(operationLogInfoDto));
        return basicAllchipsLog;
    }

    /**
     * 获取操作日志描述信息
     *
     * @Author Dawn
     * @Date 2021/10/28 11:41
     */
    public String getDesc(OperationLogInfoDto operationLogInfoDto) {
        String desc = operationLogInfoDto.getOperationLogTemplate().getDesc();
        if (CommUtils.isEmpty(desc)) {
            return "";
        }
        if (operationLogInfoDto.getDescParamMap() == null || operationLogInfoDto.getDescParamMap().size() == 0) {
            return desc;
        }

        for (Map.Entry entry : operationLogInfoDto.getDescParamMap().entrySet()) {
            desc = desc.replaceAll("%" + entry.getKey() + "%", AllchipsLogUtils.toString(entry.getValue()));
        }
        return desc;
    }
}
